home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / math / ast51src.zip / PLACALC.C < prev    next >
C/C++ Source or Header  |  1995-12-31  |  44KB  |  1,198 lines

  1. /*
  2. ** Astrolog (Version 5.10) File: placalc.c
  3. **
  4. ** IMPORTANT NOTICE: The graphics database and chart display routines
  5. ** used in this program are Copyright (C) 1991-1995 by Walter D. Pullen
  6. ** (Astara@msn.com). Permission is granted to freely use and
  7. ** distribute these routines provided one doesn't sell, restrict, or
  8. ** profit from them in any way. Modification is allowed provided these
  9. ** notices remain with any altered or edited versions of the program.
  10. **
  11. ** The main planetary calculation routines used in this program have
  12. ** been Copyrighted and the core of this program is basically a
  13. ** conversion to C of the routines created by James Neely as listed in
  14. ** Michael Erlewine's 'Manual of Computer Programming for Astrologers',
  15. ** available from Matrix Software. The copyright gives us permission to
  16. ** use the routines for personal use but not to sell them or profit from
  17. ** them in any way.
  18. **
  19. ** The PostScript code within the core graphics routines are programmed
  20. ** and Copyright (C) 1992-1993 by Brian D. Willoughby
  21. ** (brianw@sounds.wa.com). Conditions are identical to those above.
  22. **
  23. ** The extended accurate ephemeris databases and formulas are from the
  24. ** calculation routines in the program "Placalc" and are programmed and
  25. ** Copyright (C) 1989,1991,1993 by Astrodienst AG and Alois Treindl
  26. ** (alois@azur.ch). The use of that source code is subject to
  27. ** regulations made by Astrodienst Zurich, and the code is not in the
  28. ** public domain. This copyright notice must not be changed or removed
  29. ** by any user of this program.
  30. **
  31. ** Initial programming 8/28,30, 9/10,13,16,20,23, 10/3,6,7, 11/7,10,21/1991.
  32. ** X Window graphics initially programmed 10/23-29/1991.
  33. ** PostScript graphics initially programmed 11/29-30/1992.
  34. ** Last code change made 12/27/1995.
  35. */
  36.  
  37. #include "placalc.h"
  38.  
  39.  
  40. #ifdef PLACALC
  41. /*
  42. ** ---------------------------------------------------------------
  43. ** | Copyright Astrodienst AG and Alois Treindl, 1989,1991,1993  |
  44. ** | The use of this source code is subject to regulations made  |
  45. ** | by Astrodienst Zurich. The code is NOT in the public domain.|
  46. ** |                                                             |
  47. ** | This copyright notice must not be changed or removed        |
  48. ** | by any user of this program.                                |
  49. ** ---------------------------------------------------------------
  50. **
  51. ** Important changes:
  52. ** 11-jun-93 revision 1.12: fixed error which affected Mercury between -2100
  53. ** and -3100 (it jumped wildly).
  54. */
  55.  
  56. /* function calc():
  57. ** This is the main routine for computing a planets position.
  58. ** The function has several modes, which are controlled by bits in
  59. ** the parameter 'flag'. The normal mode (flag == 0) computes
  60. ** a planets apparent geocentric position in ecliptic coordinates relative to
  61. ** the true equinox of date, without speed
  62. **
  63. ** Explanation of the arguments: see the functions header.
  64. **
  65. ** Returns OK or ERR (if some planet out of time range). OK and ERR are
  66. ** defined in ourdef.h and must not be confused with TRUE and FALSE.
  67. ** OK and ERR are of type int, not of type BOOLEAN.
  68. **
  69. ** Bits used in flag:
  70. ** CALC_BIT_HELIO     0 = geocentric, 1 = heliocentric
  71. ** CALC_BIT_NOAPP       0 = apparent positions, 1 = true positions
  72. ** CALC_BIT_NONUT     0 = do nutation (true equinox of date)
  73. ** 1 = don't do nutation (mean equinox of date).
  74. **
  75. ** CALC_BIT_SPEED     0 = don't calc speed,
  76. ** 1 = calc speed, takes quite long for moon
  77. ** (is observed only for moon, with other
  78. ** planets speed is cheap)
  79. **
  80. ** Side effects and local memory:
  81. ** For doing heliocentric positions the fucntion must know the
  82. ** earth's position for the desired time t. It remembers the earth
  83. ** position so it does not have to recompute it each time a planet
  84. ** position is wanted for the same time t.
  85. ** It calls helup(t), which leaves as a side effect the global
  86. ** variables meanekl, ekl and nut for the time t.
  87. **
  88. ** Functions called by calc():
  89. ** helup(t)
  90. ** hel(t)
  91. ** moon(t)
  92. ** togeo()
  93. **
  94. ** Time range:
  95. ** The function can be used savely in the time range 5000 BC to
  96. ** 3000 AD. The stored ephemeris is available only for this time
  97. ** range, so Jupiter ... Pluto cannot be computed outside. The
  98. ** function will return results for the other planets also outside
  99. ** of this time range, but they become meaningless pretty soon
  100. ** before 5000 BC, because Newcombs time series expansions for the
  101. ** elements will not work anymore.
  102. **
  103. ** pointers to the return variables:
  104. ** alng = ecliptic longitude in degrees
  105. ** arad = radius vector in AU (astronomic units)
  106. ** alat = ecliptic latitude in degrees
  107. ** alngspeed = speed of planet in degrees per day
  108. **
  109. ** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  110. ** The precision of the speed is quite limited.
  111. ** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  112. **
  113. ** For Sun, Mercury, Venus and Mars we take only the speed from
  114. ** the undisturbed Kepler orbit. For the Moon there is no
  115. ** reasonable undisturbed orbit and we derive the speed from
  116. ** its position at t + dt and t - dt. We need these
  117. ** moon positions anyway for the true node calculation.
  118. ** For the outer planets and Chiron we derive the precise
  119. ** speed from the stored ephemeris by high order inter-
  120. ** polation; the precision is limited for the geocentric
  121. ** case due to the limited precision of the earth's/sun's speed.
  122. ** Applications who need precise speeds should
  123. ** get them by calling calc() with slightly different times.
  124. **
  125. ** Comment 7 May 1991 by Alois Treindl:
  126. ** Center of Earth versus Barycenter Earth-Moon:
  127. ** Brown's theory of the moon gives the moon's coordinates relative
  128. ** to the center of the earth. Newcomb's theory of the Sun gives the
  129. ** coordinates of the earth's center relative to the center of the Sun.
  130. ** This is what we need.
  131. **
  132. ** How about the Mean Lunar Node?
  133. ** The orbital elements of the Sun in Newcomb's theory are given
  134. ** relative to the barycenter Earth-Moon; the reduction to geocentric
  135. ** is only applied after doing the Kepler ellipse calculation.
  136. ** Are the Lunar elements also relative to the barycenter??
  137. ** If yes:
  138. ** When we use the moon's mean node out of the elements, it is still
  139. ** as seen from the barycenter. Because the node is close to the
  140. ** earth, we would have to apply a considerable correction, which is of
  141. ** the order of 4000/384000 km or 35' (minutes of arc).
  142. ** Nobody has ever applied such a correction to the mean node.
  143. **
  144. ** And the True Node?
  145. ** When we calculate the osculating orbital elements of the Moon (true node),
  146. ** are they relative to the barycenter or to the Earth's center?
  147. ** Our derivation of true node from the actual Moon positions considers
  148. ** the earth's center as the focal point of the osculating lunar ellipse.
  149. ** A more correct approach would first reduce the lunar position from
  150. ** geocentric to barycentric, then compute the orbital elements from
  151. ** the reduced positions, and then reduce the desired items
  152. ** (node, apogaeum, 'dark moon') to geocentric positions.
  153. ** No known astrological ephemeris has ever used such a correction, which is
  154. ** of the same order of magnitude as the correction to the meannode above.
  155. ** When the moon is going through the ecliptic, the geocenter, barycenter
  156. ** moon (and the node identical to the moon itself) line up; this is why
  157. ** the error does not show up in normal considerations.
  158. */
  159.  
  160. int calc(planet, jd_ad, flag, alng, arad, alat, alngspeed)
  161. int planet;  /* planet index as defined in placalc.h,
  162. SUN = 0, MOON = 1 etc.
  163. planet == -1 calc calculates only nut and ecl */
  164. REAL8 jd_ad;  /* relative Astrodienst Juldate, ephemeris time.
  165. Astrodienst Juldate is relative 31 Dec 1949, noon. */
  166. int flag;  /* See definition of flag bits above */
  167. REAL8 *alng;
  168. REAL8 *arad;
  169. REAL8 *alat;
  170. REAL8 *alngspeed;
  171. {
  172.   struct rememberdat  /* time for which the datas are calculated */
  173.     {REAL8 calculation_time, lng, rad, zet, lngspeed, radspeed, zetspeed;};
  174.   static struct rememberdat earthrem =
  175.     {HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8};
  176.   static struct rememberdat moonrem  =
  177.     {HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8};
  178.   REAL8 c, s, x, knn, knv;
  179.   REAL8 rp, zp; /* needed to call hel! */
  180.   REAL8 *azet = alat;
  181.   BOOLEAN calc_geo, calc_helio, calc_apparent, calc_speed,
  182.   calc_nut;
  183.  
  184.   /* helup checks whether it was already called with same time */
  185.   helup (jd_ad);
  186.   /* we could return now if we only wanted to compute ecl and nut */
  187.  
  188.   calc_helio =  flag & CALC_BIT_HELIO;
  189.   calc_geo = ! calc_helio;
  190.   calc_apparent = ! (flag & CALC_BIT_NOAPP);
  191.   calc_nut = ! (flag & CALC_BIT_NONUT);
  192.   calc_speed = flag & CALC_BIT_SPEED;
  193.   /*
  194.   ** it is necessary to compute EARTH in the following cases:
  195.   ** heliocentric MOON or EARTH
  196.   ** geocentric any planet except MOON or nodes or LILITH
  197.   */
  198.   if (calc_helio && (planet == MOON || planet == EARTH)
  199.     || calc_geo && planet != MOON
  200.     && planet != MEAN_NODE
  201.     && planet != TRUE_NODE
  202.     && planet != LILITH) {
  203.     if (earthrem.calculation_time != jd_ad) {
  204.       hel (EARTH, jd_ad, alng, arad, azet, alngspeed, &rp, &zp);
  205.       /* store earthdata for geocentric calculation: */
  206.       earthrem.lng = *alng;
  207.       earthrem.rad = *arad;
  208.       earthrem.zet = *azet;
  209.       earthrem.lngspeed = *alngspeed;
  210.       earthrem.radspeed = rp;
  211.       earthrem.zetspeed = zp;
  212.       earthrem.calculation_time = jd_ad;
  213.     }
  214.   }
  215.   switch(planet) {
  216.  
  217.   case EARTH: /* has been already computed */
  218.     *alng = earthrem.lng;
  219.     *arad = earthrem.rad;
  220.     *azet = earthrem.zet;
  221.     *alngspeed = earthrem.lngspeed;
  222.     rp = earthrem.radspeed;
  223.     zp = earthrem.zetspeed;
  224.     if (calc_geo) { /* SUN seen from earth */
  225.       *alng = smod8360(*alng + 180.0);
  226.       *azet = - *azet;
  227.     }
  228.     if (calc_apparent)
  229.       *alng = *alng - 0.0057683 * (*arad) * (*alngspeed);
  230.     break;
  231.  
  232.   case MOON:
  233.     moon(alng, arad, azet);
  234.     moonrem.lng = *alng;  /* moonrem will be used for TRUE_NODE */
  235.     moonrem.rad = *arad;
  236.     moonrem.zet = *azet;
  237.     *alngspeed = 12;
  238.     moonrem.calculation_time = jd_ad;
  239.     if (calc_helio || calc_speed) {/* get a second moon position */
  240.       REAL8 lng2, rad2, zet2;
  241.       helup(jd_ad + MOON_SPEED_INTERVAL);
  242.       moon(&lng2, &rad2, &zet2);
  243.       helup(jd_ad);
  244.       if (calc_helio) { /* moon as seen from sun */
  245.         togeo(earthrem.lng, -earthrem.rad, moonrem.lng, moonrem.rad,
  246.         moonrem.zet, alng, arad);
  247.         togeo(earthrem.lng + MOON_SPEED_INTERVAL * earthrem.lngspeed,
  248.         -(earthrem.rad + MOON_SPEED_INTERVAL * earthrem.radspeed),
  249.         lng2, rad2, zet2, &lng2, &rad2);
  250.       }
  251.       *alngspeed =  diff8360(lng2, *alng) / MOON_SPEED_INTERVAL;
  252.       /* rp = (rad2 - *arad) / MOON_SPEED_INTERVAL;       */
  253.       /* zp = (zet2 - moonrem.zet) / MOON_SPEED_INTERVAL; */
  254.     }
  255.     *alat = RADTODEG * ASIN8(*azet / *arad);
  256.     /*
  257.     ** light time correction, not applied for moon or nodes;
  258.     ** moon would have only term of ca. 0.02", see Expl.Sup.1961 p.109
  259.     */
  260.     break;
  261.  
  262.   case MERCURY:
  263.   case VENUS:
  264.   case MARS:
  265.   case JUPITER:
  266.   case SATURN:
  267.   case URANUS:
  268.   case NEPTUNE:
  269.   case PLUTO:
  270.   case CHIRON:
  271.   case CERES:
  272.   case PALLAS:
  273.   case JUNO:
  274.   case VESTA:
  275.     if (hel(planet, jd_ad, alng, arad, azet, alngspeed, &rp, &zp) != OK)
  276.       return ERR; /* outer planets can fail if out of ephemeris range */
  277.     if (calc_geo) {       /* geocentric */
  278.       REAL8 lng1, rad1, lng2, rad2;
  279.       togeo(earthrem.lng, earthrem.rad, *alng, *arad, *azet, &lng1, &rad1);
  280.       togeo(earthrem.lng + earthrem.lngspeed,
  281.       earthrem.rad + earthrem.radspeed,
  282.       *alng + *alngspeed, *arad + rp, *azet + zp, &lng2, &rad2);
  283.       *alng = lng1;
  284.       *arad = rad1;
  285.       *alngspeed = diff8360(lng2, lng1);
  286.       /* rp = rad2 - rad1; */
  287.     }
  288.     *alat = RADTODEG * ASIN8(*azet / *arad);
  289.     if (calc_apparent)
  290.       *alng = *alng - 0.0057683 * (*arad) * (*alngspeed);
  291.     break;
  292.  
  293.   case MEAN_NODE:
  294.     *alng = smod8360(el[MOON].kn);
  295.     /*
  296.     * the distance of the node is the 'orbital parameter' p = a (1-e^2);
  297.     * Our current use of the axis a is wrong, but is never used.
  298.     */
  299.     *arad = pd[MOON].axis;
  300.     *alat = 0.0;
  301.     *alngspeed = -0.053;
  302.     break;
  303.  
  304.   case TRUE_NODE: {
  305.     /* see comment 'Note 7 May 1991' above */
  306.     REAL8 ln, rn, zn,
  307.     lv, rv, zv,
  308.     l1, r1, z1,
  309.     xn, yn, xv, yv, r0, x0, y0;
  310.  
  311.     helup(jd_ad + NODE_INTERVAL);
  312.     moon(&ln, &rn, &zn);
  313.     helup(jd_ad - NODE_INTERVAL);
  314.     moon(&lv, &rv, &zv);
  315.     helup(jd_ad);
  316.     if (moonrem.calculation_time != jd_ad)
  317.       moon(&l1, &r1, &z1);
  318.     else {  /* moon is already calculated */
  319.       l1 = moonrem.lng;
  320.       r1 = moonrem.rad;
  321.       z1 = moonrem.zet;
  322.     }
  323.     rn = sqrt(rn * rn - zn * zn);
  324.     rv = sqrt(rv * rv - zv * zv);
  325.     r0 = sqrt(r1 * r1 - z1 * z1);
  326.     xn = rn * COS8(DEGTORAD * ln);
  327.     yn = rn * SIN8(DEGTORAD * ln);
  328.     xv = rv * COS8(DEGTORAD * lv);
  329.     yv = rv * SIN8(DEGTORAD * lv);
  330.     x0 = r0 * COS8(DEGTORAD * l1);
  331.     y0 = r0 * SIN8(DEGTORAD * l1);
  332.     x = test_near_zero(x0 * yn - xn * y0);
  333.     s = (y0 * zn - z1 * yn) / x;
  334.     c = test_near_zero((x0 * zn - z1 * xn) / x);
  335.     knn =  smod8360(RADTODEG * ATAN28(s, c)); /* = ATAN8(s / c) */
  336.     x = test_near_zero(y0 * xv - x0 * yv);
  337.     s = (yv * z1 - zv * y0) / x;
  338.     c = test_near_zero((xv * z1 - zv * x0) / x);
  339.     knv =  smod8360(RADTODEG * ATAN28(s, c));
  340.     *alng = smod8360((knv + knn) / 2);
  341.     /*
  342.     ** the distance of the node is the 'orbital parameter' p = a (1-e^2);
  343.     ** Our current use of the axis a is wrong.
  344.     */
  345.     *arad = pd[MOON].axis;
  346.     *alat = 0.0;
  347.     *alngspeed = diff8360(knn, knv) / NODE_INTERVAL;
  348.     }
  349.     break;
  350.  
  351.   case LILITH: {
  352.     /*
  353.     ** Added 22-Jun-93
  354.     ** Lilith or Dark Moon is the empty focal point of the mean lunar ellipse.
  355.     ** This is 180 degrees from the perihel.
  356.     ** Because the lunar orbit is not in the ecliptic, it must be
  357.     ** projected onto the ecliptic in the same way as the planetary orbits
  358.     ** are (see for example Montenbruck, Grundlagen der Ephemeridenrechnung).
  359.     **
  360.     ** We compute the MEAN Lilith, not the TRUE one which would have to be
  361.     ** derived in a similar way as the true node.
  362.     ** For the radius vector of Lilith we use a simple formula;
  363.     ** to get a precise value, the fact that the focal point of the ellipse
  364.     ** is not at the center of the earth but at the barycenter moon-earth
  365.     ** would have to be accounted for.
  366.     ** For the speed we always return a constant: the T term from the
  367.     ** lunar perihel.
  368.     ** Joelle de Gravelaine publishes in her book "Lilith der schwarze Mond"
  369.     ** (Astrodata, 1990) an ephemeris which gives noon (12.00) positions
  370.     ** but does not project onto the ecliptic.
  371.     ** This creates deviations
  372.     */
  373.     double arg_lat, lon, cosi;
  374.     struct elements *e = &el[MOON];
  375.     arg_lat = degnorm(e->pe - e->kn + 180.0);
  376.     cosi = COSDEG(e->in);
  377.     if (e->in == 0 || ABS8(arg_lat -  90.0) < TANERRLIMIT
  378.       || ABS8(arg_lat - 270.0) < TANERRLIMIT) {
  379.       lon = arg_lat;
  380.     } else {
  381.       lon = ATAN8(TANDEG(arg_lat) * cosi);
  382.       lon = RADTODEG * lon;
  383.       if (arg_lat > 90.0 && arg_lat < 270.0)  lon += 180.0;
  384.     }
  385.     lon = degnorm(lon + e->kn);
  386.     *alng = lon;
  387.     *alngspeed = 0.111404;  /* 6'41.05" per day */
  388.     *arad = 2 * pd[MOON].axis * e->ex;
  389.     /*
  390.     ** To test Gravalaines error, return unprojected long in alat.
  391.     ** the correct latitude would be:
  392.     ** *alat = RADTODEG * ASIN8(SINDEG(arg_lat) * SINDEG(e->in));
  393.     */
  394. #ifdef ASTROLOG
  395.     *alat = RADTODEG * ASIN8(SINDEG(arg_lat) * SINDEG(e->in));
  396. #else
  397.     *alat = degnorm(arg_lat + e->kn); /* unprojected longitude, no nut */
  398. #endif
  399.     }
  400.     break;
  401.  
  402.   default:
  403.     return ERR;
  404.   } /* end switch */
  405.  
  406.   if (calc_nut)
  407.     *alng += nut;
  408.   *alng = smod8360(*alng);  /* normalize to circle */
  409.   return OK;
  410. }
  411.  
  412.  
  413. /* helio to geocentric conversion */
  414.  
  415. void togeo(lngearth, radearth, lng, rad, zet, alnggeo, aradgeo)
  416. REAL8 lngearth;
  417. REAL8 radearth;
  418. REAL8 lng;
  419. REAL8 rad;
  420. REAL8 zet;
  421. REAL8 *alnggeo;
  422. REAL8 *aradgeo;
  423. {
  424.   REAL8 r1, x, y;
  425.  
  426.   r1 = sqrt(rad * rad - zet * zet);
  427.   x = r1 * COS8(DEGTORAD * lng) - radearth * COS8(DEGTORAD * lngearth);
  428.   y = r1 * SIN8(DEGTORAD * lng) - radearth * SIN8(DEGTORAD * lngearth);
  429.   *aradgeo = sqrt(x * x + y * y + zet * zet);
  430.   x = test_near_zero(x);
  431.   *alnggeo = smod8360(RADTODEG * ATAN28(y, x));
  432. }
  433.  
  434.  
  435. /*
  436. ** helup()
  437. ** prepares the orbital elements and the disturbation arguments for the
  438. ** inner planets and the moon. helup(t) is called by hel() and by calc().
  439. ** helup() returns its results in global variables.
  440. ** helup() remembers the t it has been called with before and does
  441. ** not recalculate its results when it is called more than once with
  442. ** the same t.
  443. */
  444.  
  445. void helup(jd_ad)
  446. REAL8 jd_ad;
  447. {
  448.   int i;
  449.   static REAL8 thelup = HUGE8;  /* is initialized only once at load time */
  450.   struct elements *e = el;      /* pointer to el[i] */
  451.   struct elements *ee = el;     /* pointer to el[EARTH] */
  452.   struct eledata  *d = pd;      /* pointer to pd[i] */
  453.   REAL8 td, ti, ti2, tj1, tj2, tj3;
  454.  
  455.   if (thelup == jd_ad)
  456.     return; /* if already calculated then return */
  457.  
  458.   for (i = SUN; i <= MARS; i++, d++, e++) {
  459.     td = jd_ad - d->epoch;
  460.     ti = e->tj = td / 36525.0;  /* julian centuries from epoch */
  461.     ti2 = ti * ti;
  462.     tj1 = ti / 3600.0;  /* used when coefficients are in seconds of arc */
  463.     tj2 = ti * tj1;
  464.     tj3 = ti * tj2;
  465.     e->lg = mod8360(d->lg0 + d->lg1 * td  + d->lg2 * tj2 + d->lg3 * tj3);
  466.     /* also with moon lg1 *td is exact to 10e-8 degrees within 5000 years */
  467.     e->pe = mod8360(d->pe0 + d->pe1 * tj1 + d->pe2 * tj2 + d->pe3 * tj3);
  468.     e->ex = d->ex0 + d->ex1 * ti + d->ex2 * ti2;
  469.     e->kn = mod8360(d->kn0 + d->kn1 * tj1 + d->kn2 * tj2 + d->kn3 * tj3);
  470.     e->in = d->in0 + d->in1 * tj1 + d->in2 * tj2;
  471.     e->ma = smod8360(e->lg - e->pe);
  472.  
  473.     if (i == MOON) {
  474.       /* calculate ekliptic according Newcomb, APAE VI,
  475.       ** and nutation according Exp.Suppl. 1961, identical
  476.       ** with Mark Potttenger elemnut()
  477.       ** all terms >= 0.01" only .
  478.       ** The 1984 IAU Theory of Nutation, as published in
  479.       ** AE 1984 suppl. has not yet been implemented
  480.       ** because it would mean to use other elements of
  481.       ** moon and sun */
  482.  
  483.       REAL8 mnode, mlong2, slong2, mg, sg, d2;
  484.  
  485.       mnode  = DEGTORAD * e->kn;        /* moon's mean node */
  486.       mlong2 = DEGTORAD * 2.0 * e->lg;  /* 2 x moon's mean longitude */
  487.       mg     = DEGTORAD * e->ma;        /* moon's mean anomaly (g1) */
  488.       slong2 = DEGTORAD * 2.0 * ee->lg; /* 2 x sun's mean longitude (L), with
  489.                                         the phase 180 deg earth-sun irrelevant
  490.                                         because 2 x 180 = 360 deg */
  491.       sg     = DEGTORAD * ee->ma; /* sun's mean anomaly = earth's */
  492.       d2     = mlong2 - slong2;   /* 2 x elongation of moon from sun */
  493.       meanekl = ekld[0] + ekld[1] * tj1 + ekld[2] * tj2 + ekld[3] * tj3;
  494.       ekl = meanekl +
  495.         (9.2100 * COS8(mnode)
  496.         - 0.0904 * COS8(2.0 * mnode)
  497.         + 0.0183 * COS8(mlong2 - mnode)
  498.         + 0.0884 * COS8(mlong2)
  499.         + 0.0113 * COS8(mlong2 + mg)
  500.         + 0.5522 * COS8(slong2)
  501.         + 0.0216 * COS8(slong2 + sg)) / 3600.0;
  502.         nut = ((-17.2327 - 0.01737 * ti) * SIN8(mnode)
  503.         + 0.2088 * SIN8(2.0 * mnode)
  504.         + 0.0675 * SIN8(mg)
  505.         - 0.0149 * SIN8(mg - d2)
  506.         - 0.0342 * SIN8(mlong2 - mnode)
  507.         + 0.0114 * SIN8(mlong2 - mg)
  508.         - 0.2037 * SIN8(mlong2)
  509.         - 0.0261 * SIN8(mlong2 + mg)
  510.         + 0.0124 * SIN8(slong2 - mnode)
  511.         + 0.0214 * SIN8(slong2 - sg)
  512.         - 1.2729 * SIN8(slong2)
  513.         - 0.0497 * SIN8(slong2 + sg)
  514.         + 0.1261 * SIN8(sg)) / 3600.0;
  515.     }
  516.   }
  517.  
  518.   /* calculate the arguments sa[] for the disturbation terms */
  519.   ti = (jd_ad - EPOCH1850) / 365.25;  /* julian years from 1850 */
  520.   for (i = 0; i < SDNUM; i++)
  521.     sa [i] = mod8360(_sd [i].sd0 + _sd [i].sd1 * ti);
  522.   /*
  523.   ** sa[2] += 0.3315 * SIN8 (DEGTORAD *(133.9099 + 38.39365 * el[SUN].tj));
  524.   **
  525.   ** correction of jupiter perturbation argument for sun from Pottenger;
  526.   ** creates only .03" and 1e-7 rad, not applied because origin unclear */
  527.   thelup = jd_ad;               /* note the last helup time */
  528. }
  529.  
  530.  
  531. /*
  532. ** hel()
  533. ** Computes the heliocentric positions for all planets except the moon.
  534. ** The outer planets from Jupiter onwards, including Chiron, are
  535. ** actually done by a subsequent call to outer_hel() which takes
  536. ** exactly the same parameters.
  537. ** hel() does true position relative to the mean ecliptic and equinox
  538. ** of date. Nutation is not added and must be done so by the caller.
  539. ** The latitude of the Sun (max. 0.5") is neglected and always returned
  540. ** as zero.
  541. **
  542. ** return: OK or ERR
  543. */
  544.  
  545. int hel(planet, t, al, ar, az, alp, arp, azp)
  546. int planet;   /* planet index as defined by placalc.h */
  547. REAL8 t;      /* relative juliand date, ephemeris time */
  548.               /* Now come 6 pointers to return values. */
  549. REAL8 *al;    /* longitude in degrees */
  550. REAL8 *ar;    /* radius in AU */
  551. REAL8 *az;    /* distance from ecliptic in AU */
  552. REAL8 *alp;   /* speed in longitude, degrees per day */
  553. REAL8 *arp;   /* speed in radius, AU per day */
  554. REAL8 *azp;   /* speed in z, AU per day */
  555. {
  556.   register struct elements *e;
  557.   register struct eledata  *d;
  558.   REAL8 lk = 0.0;
  559.   REAL8 rk = 0.0;
  560.   REAL8 b, h1, sini, sinv, cosi, cosu, cosv, man, truanom, esquare,
  561.     k8, u, up, v, vp;
  562.  
  563.   if (planet >= JUPITER)
  564.     return (outer_hel(planet, t, al, ar, az, alp, arp, azp));
  565.   if (planet < SUN || planet == MOON)
  566.     return ERR;
  567.  
  568.   e = &el[planet];
  569.   d = &pd[planet];
  570.   sini = SIN8(DEGTORAD * e->in);
  571.   cosi = COS8(DEGTORAD * e->in);
  572.   esquare = sqrt((1.0 + e->ex) / (1.0 - e->ex)); /* H6 in old version */
  573.   man = e->ma;
  574.   if (planet == EARTH)  /* some longperiodic terms in mean longitude */
  575.     man += (0.266 * SIN8 (DEGTORAD * (31.8 + 119.0 * e->tj))
  576.       + 6.40 * SIN8(DEGTORAD * (231.19 + 20.2 * e->tj))
  577.       + (1.882-0.016*e->tj) * SIN8(DEGTORAD * (57.24 + 150.27 * e->tj))
  578.       ) / 3600.0;
  579.   if (planet == MARS)  /* some longperiodic terms */
  580.     man += (0.606 * SIN8(DEGTORAD * (212.87 + e->tj * 119.051))
  581.       + 52.490 * SIN8(DEGTORAD * (47.48 + e->tj * 19.771))
  582.       +  0.319 * SIN8(DEGTORAD * (116.88 + e->tj * 773.444))
  583.       +  0.130 * SIN8(DEGTORAD * (74 + e->tj * 163))
  584.       +  0.280 * SIN8(DEGTORAD * (300 + e->tj * 40.8))
  585.       -  (37.05 +13.5 * e->tj)
  586.       ) / 3600.0;
  587.   u = fnu (man, e->ex, 0.0000003); /* error 0.001" returns radians */
  588.   cosu = COS8(u);
  589.   h1 = 1 - e->ex * cosu;
  590.   *ar = d->axis * h1;
  591.   if (ABS8(rPi - u) < TANERRLIMIT)
  592.     truanom = u; /* very close to aphel */
  593.   else
  594.     truanom = 2.0 * ATAN8(esquare * TAN8(u * 0.5)); /* true anomaly, rad*/
  595.   v = smod8360(truanom * RADTODEG + e->pe - e->kn); /* argument of latitude */
  596.   if (sini == 0.0 || ABS8(v -  90.0) < TANERRLIMIT
  597.     || ABS8(v - 270.0) < TANERRLIMIT) {
  598.     *al = v;
  599.   } else {
  600.     *al = RADTODEG * ATAN8(TAN8(v * DEGTORAD) * cosi);
  601.     if (v > 90.0 && v < 270.0)  *al += 180.0;
  602.   }
  603.   *al = smod8360(*al + e->kn);
  604.   sinv = SIN8(v * DEGTORAD);
  605.   cosv = COS8(v * DEGTORAD);
  606.   *az = *ar * sinv * sini;
  607.   b = ASIN8(sinv * sini);     /* latitude in radians */
  608.   k8 = cosv / COS8(b) * sini;
  609.   up = 360.0 / d->period / h1;    /* du/dt degrees/day */
  610.   if (ABS8(rPi - u) < TANERRLIMIT)
  611.     vp = up / esquare;  /* speed at aphel */
  612.   else
  613.     vp = up * esquare * (1 + COS8 (truanom)) / (1 + cosu);
  614.   /* dv/dt degrees/day */
  615.   *arp = d->axis * up * DEGTORAD * SIN8(u) * e->ex;
  616.   /* dr/dt AU/day */
  617.   *azp = *arp * sinv * sini + *ar * vp * DEGTORAD * cosv * sini;  /* dz/dt */
  618.   *alp = vp / cosi * (1 - k8 * k8);
  619.  
  620.   /* now come the disturbations */
  621.  
  622.   switch (planet) {
  623.     REAL8 am, mma, ema, u2;
  624.  
  625.   case EARTH:
  626.     /*
  627.     ** earth has some special moon values and a disturbation series due to the
  628.     ** planets. The moon stuff is due to the fact, that the mean elements
  629.     ** give the coordinates of the earth-moon barycenter. By adding the
  630.     ** corrections we effectively reduce to the center of the earth.
  631.     ** We neglect the correction in latitude, which is about 0.5", because
  632.     ** for astrological purposes we want the Sun to have latitude zero.
  633.     */
  634.     am = DEGTORAD * smod8360(el[MOON].lg - e->lg + 180.0); /* degrees */
  635.     mma = DEGTORAD * el[MOON].ma;
  636.     ema = DEGTORAD * e->ma;
  637.     u2 = 2.0 * DEGTORAD * (e->lg - 180.0 - el[MOON].kn); /* 2u' */
  638.     lk = 6.454 * SIN8(am)
  639.       + 0.013 * SIN8(3.0 * am)
  640.       + 0.177 * SIN8(am + mma)
  641.       - 0.424 * SIN8(am - mma)
  642.       + 0.039 * SIN8(3.0 * am - mma)
  643.       - 0.064 * SIN8(am + ema)
  644.       + 0.172 * SIN8(am - ema)
  645.       - 0.013 * SIN8(am - mma - ema)
  646.       - 0.013 * SIN8(u2);
  647.     rk = 13360 * COS8(am)
  648.       + 30    * COS8(3.0 * am)
  649.       + 370   * COS8(am + mma)
  650.       - 1330  * COS8(am - mma)
  651.       + 80    * COS8(3.0 * am - mma)
  652.       - 140   * COS8(am + ema)
  653.       + 360   * COS8(am - ema)
  654.       - 30    * COS8(am - mma - ema)
  655.       + 30    * COS8(u2);
  656.     /* long periodic term from mars 15g''' - 8g'', Vol 6 p19, p24 */
  657.     lk += 0.202 * SIN8(DEGTORAD * (315.6 + 893.3 * e->tj));
  658.     disturb(earthkor, al, ar, lk, rk, man);
  659.     break;
  660.  
  661.   case MERCURY:  /* only normal disturbation series */
  662.     disturb(mercurykor, al, ar, 0.0, 0.0, man);
  663.     break;
  664.  
  665.   case VENUS:  /* some longperiod terms and normal series */
  666.     lk = (2.761 - 0.22*e->tj) * SIN8(DEGTORAD * (237.24 + 150.27 * e->tj))
  667.     + 0.269 * SIN8(DEGTORAD * (212.2  + 119.05 * e->tj))
  668.     - 0.208 * SIN8(DEGTORAD * (175.8  + 1223.5 * e->tj));
  669.     /* make seconds */
  670.     disturb(venuskor, al, ar, lk, 0.0, man);
  671.     break;
  672.  
  673.   case MARS:  /* only normal disturbation series */
  674.     disturb(marskor, al, ar, 0.0, 0.0, man);
  675.     break;
  676.   }
  677.   return OK;
  678. }
  679.  
  680.  
  681. void disturb(k, al, ar, lk, rk, man)
  682. register struct kor *k;  /* ENDMARK-terminated array of struct kor */
  683. REAL8 *al, /* longitude in degrees, use a pointer to return value */
  684. *ar;       /* radius in AU */
  685. REAL8 lk,  /* longitude correction in SECONDS OF ARC */
  686.            /* function can be called with an lk and rk already
  687.               != 0, but no value is returned */
  688. rk,        /* radius correction in units of 9th place of log r */
  689. man;       /* mean anomaly of planet */
  690. {
  691.   REAL8 arg;
  692.   while (k->j != ENDMARK) {
  693.     arg = k->j * sa[k->k] + k->i * man;
  694.     lk += k->lampl * COS8(DEGTORAD * (k->lphase - arg));
  695.     rk += k->rampl * COS8(DEGTORAD * (k->rphase - arg));
  696.     k++;
  697.   }
  698.   *ar *= EXP10(rk * 1.0E-9);  /* 10^rk */
  699.   *al += lk / 3600.0;
  700. }
  701.  
  702.  
  703. int moon(al, ar, az)  /* return OK or ERR */
  704. REAL8 *al;
  705. REAL8 *ar;
  706. REAL8 *az;
  707. {
  708.   REAL8 a1,a2,a3,a4,a5,a6,a7,a8,a9,c2,c4,arg,b,d,f,dgc,dlm,dpm,dkm,dls;
  709.   REAL8 ca, cb, cd, f_2d, f_4d, g1c,lk,lk1,man,ms,nib,s,sinarg,sinp,sk;
  710.   REAL8 t, tb, t2c, r2rad, i1corr, i2corr, dlid;
  711.   int i;
  712.   struct elements *e;
  713.   struct m45dat   *mp;
  714. #if MOON_TEST_CORR
  715.   struct m5dat    *m5p;
  716. #endif
  717.   e = &el[MOON];
  718.   t = e->tj * 36525;  /* days from epoch 1900 */
  719.  
  720.   /* new format table II, parameters in full rotations of 360 degrees */
  721.   r2rad = 360.0 * DEGTORAD;
  722.   tb  = t * 1e-12;  /* units of 10^12 */
  723.   t2c = t * t * 1e-16;  /* units of 10^16 */
  724.   a1 = SIN8(r2rad * (0.53733431 -  10104982 * tb + 191 * t2c));
  725.   a2 = SIN8(r2rad * (0.71995354 - 147094228 * tb +  43 * t2c));
  726.   c2 = COS8(r2rad * (0.71995354 - 147094228 * tb +  43 * t2c));
  727.   a3 = SIN8(r2rad * (0.14222222 +   1536238 * tb));
  728.   a4 = SIN8(r2rad * (0.48398132 - 147269147 * tb +  43 * t2c));
  729.   c4 = COS8(r2rad * (0.48398132 - 147269147 * tb +  43 * t2c));
  730.   a5 = SIN8(r2rad * (0.52453688 - 147162675 * tb +  43 * t2c));
  731.   a6 = SIN8(r2rad * (0.84536324 -  11459387 * tb));
  732.   a7 = SIN8(r2rad * (0.23363774 +   1232723 * tb + 191 * t2c));
  733.   a8 = SIN8(r2rad * (0.58750000 +   9050118 * tb));
  734.   a9 = SIN8(r2rad * (0.61043085 -  67718733 * tb));
  735.  
  736.   dlm = 0.84 * a3 + 0.31 * a7 + 14.27 * a1 + 7.261  * a2 + 0.282 * a4
  737.     + 0.237 * a6;
  738.   dpm = -2.1  * a3 - 2.076  * a2 - 0.840 * a4 - 0.593 * a6;
  739.   dkm = 0.63 * a3 + 95.96 * a2 + 15.58 * a4 + 1.86 * a5;
  740.   dls = -6.4  * a3 - 0.27 * a8 - 1.89  * a6 + 0.20 * a9;
  741.   dgc = (-4.318 * c2 - 0.698 * c4) / 3600.0 / 360.0;  /* in revolutions */
  742.   dgc = (1.000002708 + 139.978 * dgc);  /* in this form used later */
  743.   man = DEGTORAD * (e->ma + (dlm - dpm) / 3600.0);
  744.   /* man with periodic and secular corr. */
  745.   ms  = DEGTORAD * (el[EARTH].ma + dls / 3600.0);
  746.   f   = DEGTORAD * (e->lg - e->kn + (dlm - dkm) / 3600.0);
  747.   d   = DEGTORAD * (e->lg + 180 - el[EARTH].lg + (dlm - dls) / 3600.0);
  748.  
  749.   lk = lk1 = sk = sinp = nib = g1c = 0;
  750.   i1corr = 1.0 - 6.8320E-8 * t;
  751.   i2corr = dgc * dgc; /* i2 occurs only as -2, 2 */
  752.   for (i = 0, mp = m45; i < NUM_MOON_CORR; i++, mp++) {
  753.     /* arg = mp->i0 * man + mp->i1 * ms + mp->i2 * f + mp->i3 * d; */
  754.     arg = mp->i0 * man;
  755.     arg += mp->i3 * d;
  756.     arg += mp->i2 * f;
  757.     arg += mp->i1 * ms;
  758.     sinarg = SIN8(arg);
  759.     /*
  760.     ** now apply corrections due to changes in constants;
  761.     ** we correct only terms in l' (i1) and F (i2), not in l (i0), because
  762.     ** the latter are < 0.05"
  763.     ** We don't apply corrections  for cos(arg), i.e. for parallax
  764.     */
  765.     if (mp->i1 != 0) {  /* i1 can be -2, -1, 0, 1, 2 */
  766.       sinarg *= i1corr;
  767.       if  (mp->i1 == 2 || mp->i1 == -2)
  768.         sinarg *= i1corr;
  769.     }
  770.     if (mp->i2 != 0)  /* i2 can be -2, 0, 2 */
  771.       sinarg *= i2corr;
  772.     lk += mp->lng * sinarg;
  773.     sk += mp->lat * sinarg;
  774.     sinp += mp->par * COS8 (arg) ;
  775.   }
  776.  
  777. #if MOON_TEST_CORR  /* optionally add more lunar longitudes  */
  778.   for (m5p = m5; m5p->i0 != 99; m5p++) {  /* i0 = 99 is end mark */
  779.     arg = m5p->i0 * man + m5p->i1 * ms + m5p->i2 * f + m5p->i3 * d;
  780.     sinarg = SIN8(arg);
  781.     lk1 += m5p->lng * sinarg;
  782.   }
  783. #endif
  784.  
  785.   /*
  786.   ** now compute some planetary terms in longitude, list i delta;
  787.   ** we take all > 0.5" and neglect secular terms in the arguments. These
  788.   ** produce phase errors > 10 degrees only after 3000 years.
  789.   */
  790.   dlid =  0.822 * SIN8 (r2rad * (0.32480 - 0.0017125594 * t));
  791.   dlid += 0.307 * SIN8 (r2rad * (0.14905 - 0.0034251187 * t));
  792.   dlid += 0.348 * SIN8 (r2rad * (0.68266 - 0.0006873156 * t));
  793.   dlid += 0.662 * SIN8 (r2rad * (0.65162 + 0.0365724168 * t));
  794.   dlid += 0.643 * SIN8 (r2rad * (0.88098 - 0.0025069941 * t));
  795.   dlid += 1.137 * SIN8 (r2rad * (0.85823 + 0.0364487270 * t));
  796.   dlid += 0.436 * SIN8 (r2rad * (0.71892 + 0.0362179180 * t));
  797.   dlid += 0.327 * SIN8 (r2rad * (0.97639 + 0.0001734910 * t));
  798.  
  799.   /* without nutation */
  800.   *al = smod8360(e->lg + (dlm + lk + lk1 + dlid) / 3600.0);
  801.  
  802.   /* solar Terms in latitude Nibeta */
  803.   f_2d = f - 2.0 * d;
  804.   f_4d = f - 4.0 * d;
  805.   nib += -526.069 * SIN8(                   f_2d);
  806.   nib +=   -3.352 * SIN8(                   f_4d);
  807.   nib +=   44.297 * SIN8( man             + f_2d);
  808.   nib +=   -6.000 * SIN8( man             + f_4d);
  809.   nib +=   20.599 * SIN8(-man             + f   );
  810.   nib +=  -30.598 * SIN8(-man             + f_2d);
  811.   nib +=  -24.649 * SIN8(-2*man           + f   );
  812.   nib +=   -2.000 * SIN8(-2*man           + f_2d);
  813.   nib +=  -22.571 * SIN8(          ms     + f_2d);
  814.   nib +=   10.985 * SIN8(         -ms     + f_2d);
  815.  
  816.   /* new gamma1C from 29 Jul 88, all terms > 0.4 " in table III, code 2 */
  817.   g1c += -0.725 * COS8(          d);
  818.   g1c +=  0.601 * COS8(      2 * d);
  819.   g1c +=  0.394 * COS8(      3 * d);
  820.   g1c += -0.445 * COS8(man                   + 4 * d);
  821.   g1c +=  0.455 * COS8(man                   + 1 * d);
  822.   g1c +=  5.679 * COS8(2 * man               - 2 * d);
  823.   g1c += -1.300 * COS8(3 * man                      );
  824.   g1c += -1.302 * COS8(            ms               );
  825.   g1c += -0.416 * COS8(            ms        - 4 * d);
  826.   g1c += -0.740 * COS8(        2 * ms        - 2 * d);
  827.   g1c +=  0.787 * COS8(    man +   ms        + 2 * d);
  828.   g1c +=  0.461 * COS8(    man +   ms               );
  829.   g1c +=  2.056 * COS8(    man +   ms        - 2 * d);
  830.   g1c += -0.471 * COS8(    man +   ms        - 4 * d);
  831.   g1c += -0.443 * COS8(   -man +   ms        + 2 * d);
  832.   g1c +=  0.679 * COS8(   -man +   ms               );
  833.   g1c += -1.540 * COS8(   -man +   ms        - 2 * d);
  834.  
  835.   s =  f + sk / 3600.0 * DEGTORAD;
  836.   ca = 18519.7 + g1c;
  837.   cb = -0.000336992 * ca * dgc * dgc * dgc;
  838.   cd = ca / 18519.7;
  839.   b = (ca * SIN8(s) * dgc  + cb * SIN8(3.0 * s) + cd * nib) / 3600.0;
  840.  
  841.   /* we neglect the planetary terms in latitude, code 4 in table III */
  842.  
  843.   sinp = (sinp + 3422.451);
  844.   /*
  845.   ** Improved lunar ephemeris and APAE until ca. 1970 had here
  846.   ** 3422.54 as constant of moon's sine parallax.
  847.   ** The difference can be applied by direct addition of 0.089" to
  848.   ** our parallax results.
  849.   **
  850.   ** To get the radius in A.U. from the sine parallax,
  851.   ** we use 1964 IAU value 8.794" for solar parallax.
  852.   ** sinp is still in seconds of arc.
  853.   ** To calculate moon parallax in " it would be:
  854.   ** p = sinp (1  + sinp * sinp * 3.917405E-12)
  855.   ** based on the formula p = sinp + 1/6 sinp^3
  856.   ** and taking into account the conversion of " to radians.
  857.   ** The semidiameter of the moon is: (Expl.Suppl. 61, p 109)
  858.   ** s = 0.0796 + 0.272446 * p
  859.   */
  860.  
  861.   *ar = 8.794 / sinp;
  862.   *az = *ar * SIN8(DEGTORAD * b);
  863.   return OK;
  864. }
  865.  
  866.  
  867. /*
  868. ** outer_hel()
  869. ** Computes the position of Jupiter, Saturn, Uranus, Neptune, Pluto and
  870. ** Chiron by reading our stored ephemeris in steps of 80 (!) days and
  871. ** applying a high order interpolation to it. The interpolation errors are
  872. ** less than 0.01" seconds of arc.
  873. ** The stored ephemeris is packed in a special format consisting of
  874. ** 32 bit numbers; it has been created on the Astrodienst Unix system
  875. ** by numerical integration with routines provided originally by Marc
  876. ** Pottenger, USA, which we improved for better long term precision.
  877. ** Because the Unix system uses a different byte order than the MSDOS
  878. ** systems, the bytes must be reordered for MSDOS after reading from
  879. ** the binary files.
  880. **
  881. ** outer_hel() takes the same parameters as hel().
  882. ** It returns the same type of values.
  883. **
  884. ** The access to the ephemeris files is done in the functions chi_file_posit()
  885. ** and lrz_file_posit().
  886. */
  887.  
  888. int outer_hel(planet, jd_ad, al, ar, az, alp, arp, azp)
  889. int planet;
  890. REAL8 jd_ad;  /* jd_ad Astrodienst relative Julian ephemeris time */
  891. REAL8 *al;
  892. REAL8 *ar;
  893. REAL8 *az;
  894. REAL8 *alp;
  895. REAL8 *arp;
  896. REAL8 *azp;
  897. {
  898.   static FILE *outerfp = NULL, *chironfp = NULL, *asterfp = NULL;
  899.   static double last_j0_outer = HUGE8;
  900.   static double last_j0_chiron = HUGE8;
  901.   static double last_j0_aster = HUGE8;
  902.   static long icoord[6][5][3], chicoord[6][3], ascoord[6][4][3];
  903.   REAL8 j0, jd, jfrac;
  904.   REAL8 l[6], r[6], z[6];
  905.   int n, order, p;
  906.  
  907.   if ((planet < JUPITER || planet > PLUTO) && planet != CHIRON &&
  908.     (planet < CERES || planet > VESTA))
  909.     return ERR;
  910.   jd = jd_ad + JUL_OFFSET;
  911.   j0 = RFloor((jd - 0.5) / EPHE_STEP) * EPHE_STEP + 0.5;
  912.   jfrac = (jd - j0) / EPHE_STEP;
  913.   if (planet == CHIRON) {
  914.     if (last_j0_chiron != j0) {
  915.       for (n = 0; n < 6; n++) { /* read 6 days */
  916.         jd = j0 + (n - 2) * EPHE_STEP;
  917.         if (chi_file_posit(jd, &chironfp) != OK)
  918.           return ERR;
  919.         fread(&chicoord[n][0], sizeof(word4), 3, chironfp);
  920.         longreorder((UCHAR *)&chicoord[n][0], 3*4);
  921.       }
  922.       last_j0_chiron = j0;
  923.     }
  924.     for (n = 0; n < 6; n++) {
  925.       l[n] = chicoord[n][0] / DEG2MSEC;
  926.       r[n] = chicoord[n][1] / AU2INT;
  927.       z[n] = chicoord[n][2] / AU2INT;
  928.     }
  929.   } else if (planet >= CERES && planet <= VESTA) {
  930.     if (last_j0_aster != j0) {  /* read all 4 asteroids for 6 steps */
  931.       for (n = 0; n < 6; n++) {
  932.         jd = j0 + (n - 2) * EPHE_STEP;
  933.         if (ast_file_posit(jd, &asterfp) != OK)
  934.           return ERR;
  935.         fread(&ascoord[n][0][0], sizeof(word4), 12, asterfp);
  936.         longreorder((UCHAR *)&ascoord[n][0][0], 12*4);
  937.       }
  938.       last_j0_outer = j0;
  939.     }
  940.     p = planet - CERES;
  941.     for (n = 0; n < 6; n++) {
  942.       l[n] = ascoord[n][p][0] / DEG2MSEC;
  943.       r[n] = ascoord[n][p][1] / AU2INT;
  944.       z[n] = ascoord[n][p][2] / AU2INT;
  945.     }
  946.   } else {  /* an outerplanet */
  947.     if (last_j0_outer != j0) {  /* read all 5 planets for 6 steps */
  948.       for (n = 0; n < 6; n++) {
  949.         jd = j0 + (n - 2) * EPHE_STEP;
  950.         if (lrz_file_posit(jd, &outerfp) != OK)
  951.           return ERR;
  952.         fread(&icoord[n][0][0], sizeof(word4), 15, outerfp);
  953.         longreorder((UCHAR *)&icoord[n][0][0], 15*4);
  954.       }
  955.       last_j0_outer = j0;
  956.     }
  957.     p = planet - JUPITER;
  958.     for (n = 0; n < 6; n++) {
  959.       l[n] = icoord[n][p][0] / DEG2MSEC;
  960.       r[n] = icoord[n][p][1] / AU2INT;
  961.       z[n] = icoord[n][p][2] / AU2INT;
  962.     }
  963.   }
  964.   if (planet > SATURN)
  965.     order = 3;
  966.   else
  967.     order = 5;
  968.   inpolq(2, order, jfrac, l, al, alp);
  969.   *alp /= EPHE_STEP;
  970.   inpolq(2, order, jfrac, r, ar, arp);
  971.   *arp /= EPHE_STEP;
  972.   inpolq(2, order, jfrac, z, az, azp);
  973.   *azp /= EPHE_STEP;
  974.   return OK;
  975. }
  976.  
  977.  
  978. /*
  979. ** quicker Everett interpolation, after Pottenger
  980. ** version 9 Jul 1988 by Alois Treindl
  981. ** return OK or ERR.
  982. */
  983.  
  984. int inpolq(n, o, p, x, axu, adxu)
  985. int n,     /* interpolate between x[n] and x[n-1], at argument n+p */
  986. o;         /* order of interpolation, maximum 5 */
  987. double p,  /* argument , intervall [0..1] */
  988. x[],       /* array of function values, x[n-o]..x[n+o] must exist */
  989. *axu,      /* pointer for storage of result */
  990. *adxu;     /* pointer for storage of dx/dt  */
  991. {
  992.   static double q, q2, q3, q4, q5, p2, p3, p4, p5, u, u0, u1, u2;
  993.   static double lastp = 9999;
  994.   double dm2, dm1, d0, dp1, dp2,
  995.     d2m1, d20, d2p1, d2p2, d30, d3p1, d3p2, d4p1, d4p2;
  996.   double offset = 0.0;
  997.  
  998.   if (lastp != p) {
  999.     q  = 1.0-p;
  1000.     q2 = q*q;
  1001.     q3 = (q+1.0)*q*(q-1.0)/6.0;       /* q - 1 over 3; u5 */
  1002.     p2 = p*p;
  1003.     p3 = (p+1.0)*p*(p-1.0)/6.0;       /* p - 1 over 3; u8 */
  1004.     u  = (3.0*p2-1.0)/6.0;
  1005.     u0 = (3.0*q2-1.0)/6.0;
  1006.     q4 = q2*q2;                       /* f5 */
  1007.     p4 = p2*p2;                       /* f4 */
  1008.     u1 = (5.0*p4-15.0*p2+4.0)/120.0;  /* u1 */
  1009.     u2 = (5.0*q4-15.0*q2+4.0)/120.0;  /* u2 */
  1010.     q5 = q3*(q+2.0)*(q-2.0)/20.0;     /* q - 2 over 5; u6 */
  1011.     p5 = (p+2.0)*p3*(p-2.0)/20.0;     /* p - 2 over 5; u9 */
  1012.     lastp = p;
  1013.   }
  1014.   dm1 = x[n] - x[n-1];
  1015.   if (dm1 > 180.0)
  1016.     dm1 -= 360.0;
  1017.   if (dm1 < -180.0)
  1018.     dm1 += 360.0;
  1019.   d0 = x[n+1] - x[n];
  1020.   if (d0 > 180.0) {
  1021.     d0 -= 360.0;
  1022.     offset = 360.0;
  1023.   }
  1024.   if (d0 < -180.0) {
  1025.     d0 += 360.0;
  1026.     offset = -360.0;
  1027.   }
  1028.   dp1 = x[n+2] - x[n+1];
  1029.   if (dp1 > 180.0)
  1030.     dp1 -= 360.0;
  1031.   if (dp1 < -180.0)
  1032.     dp1 += 360.0;
  1033.   d20  = d0 - dm1;    /* f8 */
  1034.   d2p1 = dp1 - d0;    /* f9 */
  1035.  
  1036.   /* Everett interpolation 3rd order */
  1037.  
  1038.   *axu = q*(x[n] + offset) + q3*d20 + p*x[n+1] + p3*d2p1;
  1039.   *adxu = d0 + u*d2p1 - u0*d20;
  1040.   if (o > 3) {    /* 5th order */
  1041.     dm2 = x[n-1] - x[n-2];
  1042.     if (dm2 > 180.0)
  1043.       dm2 -= 360.0;
  1044.     if (dm2 < -180.0)
  1045.       dm2 += 360.0;
  1046.     dp2 = x[n+3] - x[n+2];
  1047.     if (dp2 > 180.0)
  1048.       dp2 -= 360.0;
  1049.     if (dp2 < -180.0)
  1050.       dp2 += 360.0;
  1051.     d2m1 = dm1 - dm2;
  1052.     d2p2 = dp2 - dp1;
  1053.     d30  = d20 - d2m1;
  1054.     d3p1 = d2p1 - d20;
  1055.     d3p2 = d2p2 - d2p1;
  1056.     d4p1 = d3p1 - d30;     /* f7 */
  1057.     d4p2 = d3p2 - d3p1;    /* f */
  1058.     *axu  += p5*d4p2 + q5*d4p1;
  1059.     *adxu += u1*d4p2 - u2*d4p1;
  1060.   }
  1061.   return OK;
  1062. }
  1063.  
  1064.  
  1065. /*
  1066. ** position lrz file at proper position for julian date jd;
  1067. ** Return OK or ERR.  Version for outer planets.
  1068. */
  1069.  
  1070. int lrz_file_posit(jd, lrzfpp)
  1071. double jd;     /* full Julian day number, not Astrodienst relative */
  1072. FILE **lrzfpp; /* pointer to file pointer; this function
  1073.                   opens or closes the ephemeris file, and caller
  1074.                   should keep it open while using it. The caller
  1075.                   should close it when he is finished with using
  1076.                   the placalc() package. */
  1077. {
  1078.   int filenr;
  1079.   long posit, jlong;
  1080.   char fname[cchSzDef];
  1081.   static int open_lrznr = -10000; /* local memory to remember whether
  1082.     an already open file is the one with
  1083.     the correct number for this date */
  1084.  
  1085.   jlong = (long)RFloor(jd);
  1086.   filenr = (int)(jlong / EPHE_DAYS_PER_FILE);
  1087.   if (jlong < 0 && filenr * EPHE_DAYS_PER_FILE != jlong)
  1088.     filenr--;
  1089.   posit = jlong - filenr * EPHE_DAYS_PER_FILE;
  1090.   posit = (posit / (int)EPHE_STEP) * EPHE_OUTER_BSIZE;
  1091.   if (*lrzfpp == NULL || open_lrznr != filenr) { /* no or wrong open file */
  1092.     open_lrznr = -10000;
  1093.     sprintf(fname, "%s%s%d", EPHE_OUTER, filenr < 0 ? "M" : "", abs(filenr));
  1094.     if (*lrzfpp != NULL)
  1095.       fclose(*lrzfpp);
  1096.     *lrzfpp = FileOpen(fname, 2);
  1097.     if (*lrzfpp == NULL) {
  1098.       ErrorEphem(fname, -1);
  1099.       return ERR;
  1100.     }
  1101.     open_lrznr = filenr;
  1102.   }
  1103.   if (fseek(*lrzfpp, posit, 0) == 0)
  1104.     return OK;
  1105.   ErrorEphem(fname, posit);
  1106.   return ERR; /* this fseek error occurs only with incomplete files */
  1107. }
  1108.  
  1109.  
  1110. /*
  1111. ** position cpjv file at proper position for julian date jd;
  1112. ** Return OK or ERR.  Version for asteroids.
  1113. ** Sister function to lrz_file_posit().
  1114. */
  1115.  
  1116. int ast_file_posit(jd, astfpp)
  1117. double jd;     /* full Julian day number, not Astrodienst relative */
  1118. FILE **astfpp; /* pointer to file pointer; this function
  1119.                   opens or closes the ephemeris file, and caller
  1120.                   should keep it open while using it. */
  1121. {
  1122.   int filenr;
  1123.   long posit, jlong;
  1124.   char fname[cchSzDef];
  1125.   static int open_astnr = -10000; /* local memory to remember whether
  1126.     an already open file is the one with
  1127.     the correct number for this date */
  1128.  
  1129.   jlong = (long)RFloor(jd);
  1130.   filenr = (int)(jlong / EPHE_DAYS_PER_FILE);
  1131.   if (jlong < 0 && filenr * EPHE_DAYS_PER_FILE != jlong)
  1132.     filenr--;
  1133.   posit = jlong - filenr * EPHE_DAYS_PER_FILE;
  1134.   posit = (posit / (int)EPHE_STEP) * EPHE_ASTER_BSIZE;
  1135.   if (*astfpp == NULL || open_astnr != filenr) { /* no or wrong open file */
  1136.     open_astnr = -10000;
  1137.     sprintf(fname, "%s%s%d", EPHE_ASTER, filenr < 0 ? "M" : "", abs(filenr));
  1138.     if (*astfpp != NULL)
  1139.       fclose(*astfpp);
  1140.     *astfpp = FileOpen(fname, 2);
  1141.     if (*astfpp == NULL) {
  1142.       ErrorEphem(fname, -1);
  1143.       return ERR;
  1144.     }
  1145.     open_astnr = filenr;
  1146.   }
  1147.   if (fseek(*astfpp, posit, 0) == 0)
  1148.     return OK;
  1149.   ErrorEphem(fname, posit);
  1150.   return ERR; /* this fseek error occurs only with incomplete files */
  1151. }
  1152.  
  1153.  
  1154. /*
  1155. ** position chiron file at proper position for julian date jd;
  1156. ** Return OK or ERR. Version for Chiron.
  1157. */
  1158.  
  1159. int chi_file_posit(jd, lrzfpp)
  1160. double jd;  /* full Julian day number, not Astrodienst relative */
  1161. FILE **lrzfpp; /* pointer to file pointer; this function
  1162.                   opens or closes the ephemeris file, and caller
  1163.                   should keep it open while using it */
  1164. {
  1165.   int filenr;
  1166.   long posit, jlong;
  1167.   char fname[cchSzDef];
  1168.   static int open_lrznr = -10000; /* local memory to remember whether
  1169.     an already open file is the one with
  1170.     the correct number for this date */
  1171.  
  1172.   jlong = (long)RFloor(jd);
  1173.   filenr = (int)(jlong / EPHE_DAYS_PER_FILE);
  1174.   if (jlong < 0 && filenr * EPHE_DAYS_PER_FILE != jlong)
  1175.     filenr--;
  1176.   posit = jlong - filenr * EPHE_DAYS_PER_FILE;
  1177.   posit = (posit / (int)EPHE_STEP) * EPHE_CHIRON_BSIZE;
  1178.   if (*lrzfpp == NULL || open_lrznr != filenr) { /* no or wrong open file */
  1179.     open_lrznr = -10000;
  1180.     sprintf(fname, "%s%s%d", EPHE_CHIRON, filenr < 0 ? "M" : "", abs(filenr));
  1181.     if (*lrzfpp != NULL)
  1182.       fclose(*lrzfpp);
  1183.     *lrzfpp = FileOpen(fname, 2);
  1184.     if (*lrzfpp == NULL) {
  1185.       ErrorEphem(fname, -1);
  1186.       return ERR;
  1187.     }
  1188.     open_lrznr = filenr;
  1189.   }
  1190.   if (fseek (*lrzfpp, posit, 0) == 0)
  1191.     return OK;
  1192.   ErrorEphem(fname, posit);
  1193.   return ERR; /* this fseek error occurs only with incomplete files */
  1194. }
  1195. #endif /* PLACALC */
  1196.  
  1197. /* placalc.c */
  1198.